//
//  YKTimeView.m
//  AnchorPoint
//
//  Created by 吴高烽 on 2016/11/24.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKTimeView.h"
@interface YKTimeView()
/** <#注释#> */
@property (nonatomic ,assign)BOOL flag;
@property (nonatomic ,assign)NSInteger second;

@end

@implementation YKTimeView
-(void)awakeFromNib{
    [super awakeFromNib];
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self setUpAllChilen];
    }
    return self;
}
-(void)setUpAllChilen{
    self.backgroundColor = [UIColor whiteColor];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];

}
-(void)layoutSubviews{
    [super layoutSubviews];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
//    CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
    CGFloat centerX = rect.size.width * 0.5 ;
    CGFloat centerY = rect.size.height *0.5 ;
    CGFloat radius = rect.size.width * 0.4 ;
    CGFloat angle = M_PI_2/3 ;
    CGFloat startAngle = -M_PI_2 ;
    CGFloat endAngle = startAngle + angle ;
        CGContextRef ctf = UIGraphicsGetCurrentContext() ;
        CGContextAddArc(ctf, centerX, centerY, 2, 0, 2*M_PI, 0);
        CGContextFillPath(ctf);
        for (NSInteger i = 0 ; i < 12 ; i++) {
            CGFloat dX = radius*sin(i*angle) ;
            CGFloat dY = radius*cos(i*angle) ;
            CGContextMoveToPoint(ctf, centerX+dX*0.9, centerY-dY*0.9);
            CGContextAddLineToPoint(ctf, centerX+dX, centerY-dY);
            CGContextAddArc(ctf, centerX, centerY, radius, startAngle, endAngle, 0);
            startAngle = endAngle ;
            endAngle = startAngle + angle ;
            NSString *num ;
            if (i) {
                num = [NSString stringWithFormat:@"%ld",i];
            }else{
                num = [NSString stringWithFormat:@"%d",12];
            }
            [num drawInRect:CGRectMake(centerX+dX*0.82-5, centerY-dY*0.82-5, 15, 14) withAttributes:nil];
        }
    CGContextMoveToPoint(ctf, centerX, centerY);
    CGContextAddLineToPoint(ctf, centerX+radius*0.7*sinf(M_PI_2/15*self.second), centerY-radius*0.7*cosf(M_PI_2/15*self.second));
    CGContextStrokePath(ctf);
    self.second++;

}
//-(void)drawTimeHand{
//    CGFloat centerX = self.centerX;
//    CGFloat centerY = self.centerY;
//    CGContextRef ctf = UIGraphicsGetCurrentContext() ;
//    CGContextMoveToPoint(ctf, centerX, centerY);
//    CGContextAddLineToPoint(ctf, centerX, centerY-self.frame.size.width*0.32);
//    CGContextStrokePath(ctf);
//}

@end
